package xm.chengdu.com.beijing_211;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import com.sdwfqin.cbt.Content;
import utils.FaSong;
import utils.SpfUtils;

public class ScheduleActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.fuwuqi)
    TextView fuwuqi;
    @BindView(R.id.check1)
    android.support.v7.widget.AppCompatCheckBox check1;
    @BindView(R.id.shi1)
    EditText shi1;
    @BindView(R.id.mao1)
    TextView mao1;
    @BindView(R.id.fen1)
    EditText fen1;
    @BindView(R.id.fuwuqi2)
    TextView fuwuqi2;
    @BindView(R.id.check2)
    android.support.v7.widget.AppCompatCheckBox check2;
    @BindView(R.id.shi2)
    EditText shi2;
    @BindView(R.id.mao2)
    TextView mao2;
    @BindView(R.id.fen2)
    EditText fen2;
    @BindView(R.id.fuwuqi3)
    TextView fuwuqi3;
    @BindView(R.id.check3)
    android.support.v7.widget.AppCompatCheckBox check3;
    @BindView(R.id.shi3)
    EditText shi3;
    @BindView(R.id.mao3)
    TextView mao3;
    @BindView(R.id.fen3)
    EditText fen3;
    @BindView(R.id.fuwuqi4)
    TextView fuwuqi4;
    @BindView(R.id.check4)
    android.support.v7.widget.AppCompatCheckBox check4;
    @BindView(R.id.shi4)
    EditText shi4;
    @BindView(R.id.mao4)
    TextView mao4;
    @BindView(R.id.fen4)
    EditText fen4;
    @BindView(R.id.fuwuqi5)
    TextView fuwuqi5;
    @BindView(R.id.check5)
    android.support.v7.widget.AppCompatCheckBox check5;
    @BindView(R.id.shi5)
    EditText shi5;
    @BindView(R.id.mao5)
    TextView mao5;
    @BindView(R.id.fen5)
    EditText fen5;
    @BindView(R.id.fuwuqi6)
    TextView fuwuqi6;
    @BindView(R.id.check6)
    android.support.v7.widget.AppCompatCheckBox check6;
    @BindView(R.id.shi6)
    EditText shi6;
    @BindView(R.id.mao6)
    TextView mao6;
    @BindView(R.id.fen6)
    EditText fen6;
    @BindView(R.id.fuwuqi7)
    TextView fuwuqi7;
    @BindView(R.id.check7)
    android.support.v7.widget.AppCompatCheckBox check7;
    @BindView(R.id.shi7)
    EditText shi7;
    @BindView(R.id.mao7)
    TextView mao7;
    @BindView(R.id.fen7)
    EditText fen7;
    @BindView(R.id.home)
    RoundButton home;
    boolean chuan1 = false;
    boolean chuan2 = false;
    boolean chuan3 = false;
    boolean chuan4 = false;
    boolean chuan5 = false;
    boolean chuan6 = false;
    boolean chuan7 = false;
    StringBuilder stringBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        boolean xuan1s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan1", false);
        boolean xuan2s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan2", false);
        boolean xuan3s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan3", false);
        boolean xuan4s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan4", false);
        boolean xuan5s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan5", false);
        boolean xuan6s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan6", false);
        boolean xuan7s = (boolean) SpfUtils.get(ScheduleActivity.this, "xuan7", false);
        if (xuan1s) {
            check1.setChecked(true);
        } else {
            check1.setChecked(false);
        }
        if (xuan2s) {
            check2.setChecked(true);
        } else {
            check2.setChecked(false);
        }
        if (xuan3s) {
            check3.setChecked(true);
        } else {
            check3.setChecked(false);
        }
        if (xuan4s) {
            check4.setChecked(true);
        } else {
            check4.setChecked(false);
        }
        if (xuan5s) {
            check5.setChecked(true);
        } else {
            check5.setChecked(false);
        }
        if (xuan6s) {
            check6.setChecked(true);
        } else {
            check6.setChecked(false);
        }
        if (xuan7s) {
            check7.setChecked(true);
        } else {
            check7.setChecked(false);
        }
        stringBuilder = new StringBuilder();
        check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan1 = true;
                } else {
                    chuan1 = false;
                }

            }
        });
        check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan2 = true;
                } else {
                    chuan2 = false;
                }
            }
        });
        check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan3 = true;
                } else {
                    chuan3 = false;
                }
            }
        });
        check4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan4 = true;
                } else {
                    chuan4 = false;
                }
            }
        });
        check5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan5 = true;
                } else {
                    chuan5 = false;
                }
            }
        });
        check6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan6 = true;
                } else {
                    chuan6 = false;
                }
            }
        });
        check7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    chuan7 = true;
                } else {
                    chuan7 = false;
                }
            }
        });
        String shi1s = (String) SpfUtils.get(ScheduleActivity.this, "shi1", "");
        String fen1s = (String) SpfUtils.get(ScheduleActivity.this, "fen1", "");
        shi1.setText(shi1s);
        fen1.setText(fen1s);
        String shi2s = (String) SpfUtils.get(ScheduleActivity.this, "shi2", "");
        String fen2s = (String) SpfUtils.get(ScheduleActivity.this, "fen2", "");
        shi2.setText(shi2s);
        fen2.setText(fen2s);
        String shi3s = (String) SpfUtils.get(ScheduleActivity.this, "shi3", "");
        String fen3s = (String) SpfUtils.get(ScheduleActivity.this, "fen3", "");
        shi3.setText(shi3s);
        fen3.setText(fen3s);
        String shi4s = (String) SpfUtils.get(ScheduleActivity.this, "shi4", "");
        String fen4s = (String) SpfUtils.get(ScheduleActivity.this, "fen4", "");
        shi4.setText(shi4s);
        fen4.setText(fen4s);

        String shi5s = (String) SpfUtils.get(ScheduleActivity.this, "shi5", "");
        String fen5s = (String) SpfUtils.get(ScheduleActivity.this, "fen5", "");
        shi5.setText(shi5s);
        fen5.setText(fen5s);
        String shi6s = (String) SpfUtils.get(ScheduleActivity.this, "shi6", "");
        String fen6s = (String) SpfUtils.get(ScheduleActivity.this, "fen6", "");
        shi6.setText(shi6s);
        fen6.setText(fen6s);
        String shi7s = (String) SpfUtils.get(ScheduleActivity.this, "shi7", "");
        String fen7s = (String) SpfUtils.get(ScheduleActivity.this, "fen7", "");
        shi7.setText(shi7s);
        fen7.setText(fen7s);

    }

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("settingsuccess")) {

                SpfUtils.put(ScheduleActivity.this, "shi4", shi4.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen4", fen4.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "shi3", shi3.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen3", fen3.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "shi2", shi2.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen2", fen2.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "shi1", shi1.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen1", fen1.getText().toString());

                SpfUtils.put(ScheduleActivity.this, "shi5", shi5.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen5", fen5.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "shi6", shi6.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen6", fen6.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "shi7", shi7.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "fen7", fen7.getText().toString());
                SpfUtils.put(ScheduleActivity.this, "xuan1", check1.isChecked());
                SpfUtils.put(ScheduleActivity.this, "xuan2", check2.isChecked());
                SpfUtils.put(ScheduleActivity.this, "xuan3", check3.isChecked());
                SpfUtils.put(ScheduleActivity.this, "xuan4", check4.isChecked());
                SpfUtils.put(ScheduleActivity.this, "xuan5", check5.isChecked());
                SpfUtils.put(ScheduleActivity.this, "xuan6", check6.isChecked());
                SpfUtils.put(ScheduleActivity.this, "xuan7", check7.isChecked());
                finish();
            } else if (event.getType().equals("settingerr")) {
                shi1.setText("");
                shi2.setText("");
                shi3.setText("");
                shi4.setText("");
                shi5.setText("");
                shi6.setText("");
                shi7.setText("");
                fen1.setText("");
                fen2.setText("");
                fen3.setText("");
                fen4.setText("");
                fen5.setText("");
                fen6.setText("");
                fen7.setText("");
            }


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.img_back, R.id.home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.home:


                fasong();


                break;
        }
    }

    public void fasong() {
        if (!Content.isConn) {
            Toast.makeText(this, "Please connect to Bluetooth first.", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!chuan1 && !chuan2 && !chuan3 && !chuan4 && !chuan5 && !chuan6 && !chuan7) {
            Toast.makeText(this, "Please choose week!", Toast.LENGTH_SHORT).show();
            return;
        }
        stringBuilder.delete(0, stringBuilder.length());
        if (chuan1) {
            if (TextUtils.isEmpty(shi1.getText().toString()) || shi1.getText().toString().length() != 2 || fen1.getText().toString().length() != 2 || TextUtils.isEmpty(fen1.getText().toString())) {
                Toast.makeText(this, "Monday time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi1.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Monday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen1.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Monday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }

        }
        if (chuan2) {
            if (TextUtils.isEmpty(shi2.getText().toString()) || shi2.getText().toString().length() != 2 || fen2.getText().toString().length() != 2 || TextUtils.isEmpty(fen2.getText().toString())) {
                Toast.makeText(this, "Tuesday time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi2.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Tuesday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen2.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Tuesday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (chuan3) {
            if (TextUtils.isEmpty(shi3.getText().toString()) || shi3.getText().toString().length() != 2 || fen3.getText().toString().length() != 2 || TextUtils.isEmpty(fen3.getText().toString())) {
                Toast.makeText(this, "Wednesday time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi3.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Wednesday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen3.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Wednesday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (chuan4) {
            if (TextUtils.isEmpty(shi4.getText().toString()) || shi4.getText().toString().length() != 2 || fen4.getText().toString().length() != 2 || TextUtils.isEmpty(fen4.getText().toString())) {
                Toast.makeText(this, "Thursday time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi4.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Thursday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen4.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Thursday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (chuan5) {
            if (TextUtils.isEmpty(shi5.getText().toString()) || shi5.getText().toString().length() != 2 || fen5.getText().toString().length() != 2 || TextUtils.isEmpty(fen5.getText().toString())) {
                Toast.makeText(this, "Friday time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi5.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Friday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen5.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Friday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (chuan6) {
            if (TextUtils.isEmpty(shi6.getText().toString()) || shi6.getText().toString().length() != 2 || fen6.getText().toString().length() != 2 || TextUtils.isEmpty(fen6.getText().toString())) {
                Toast.makeText(this, "Saturday  time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi6.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Saturday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen6.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Saturday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (chuan7) {
            if (TextUtils.isEmpty(shi7.getText().toString()) || shi7.getText().toString().length() != 2 || fen7.getText().toString().length() != 2 || TextUtils.isEmpty(fen7.getText().toString())) {
                Toast.makeText(this, "Sunday  time can not be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(shi7.getText().toString());
            if (i > 24) {
                Toast.makeText(this, "Sunday time No more than 24", Toast.LENGTH_SHORT).show();
                return;
            }
            int fi = Integer.parseInt(fen7.getText().toString());
            if (fi > 59) {
                Toast.makeText(this, "Sunday time can No more than 59", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        String cishu = (String) SpfUtils.get(ScheduleActivity.this, "cishu", "1");


        if (chuan1) {

            stringBuilder.append(shi1.getText().toString());
            stringBuilder.append(fen1.getText().toString());
            stringBuilder.append("0" + cishu);

        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        if (chuan2) {

            stringBuilder.append(shi2.getText().toString());
            stringBuilder.append(fen2.getText().toString());
            stringBuilder.append("0" + cishu);
        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        if (chuan3) {

            stringBuilder.append(shi3.getText().toString());
            stringBuilder.append(fen3.getText().toString());
            stringBuilder.append("0" + cishu);
        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        if (chuan4) {

            stringBuilder.append(shi4.getText().toString());
            stringBuilder.append(fen4.getText().toString());

            stringBuilder.append("0" + cishu);
        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        if (chuan5) {

            stringBuilder.append(shi5.getText().toString());
            stringBuilder.append(fen5.getText().toString());

            stringBuilder.append("0" + cishu);
        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        if (chuan6) {

            stringBuilder.append(shi6.getText().toString());
            stringBuilder.append(fen6.getText().toString());

            stringBuilder.append("0" + cishu);
        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        if (chuan7) {

            stringBuilder.append(shi7.getText().toString());
            stringBuilder.append(fen7.getText().toString());

            stringBuilder.append("0" + cishu);
        } else {
            stringBuilder.append("00");
            stringBuilder.append("00");
            stringBuilder.append("00");
        }
        Log.e("8888", "命令" + stringBuilder.toString());
        String et_yuyuess = FaSong.yuyue(stringBuilder.toString());
        MyEvent eventet_yuyuess = new MyEvent();
        eventet_yuyuess.setType("message");
        eventet_yuyuess.setContent(et_yuyuess);
        EventBus.getDefault().post(eventet_yuyuess);
    }

}
